<include file="public/header" title="设置支付密码" body=""/>
<include file="public/header_nav" title="设置支付密码"  href="{:U('Mobile/User/userinfo')}"/>
<style>
    /*.fetchcode{
        background-color: #ec5151;
        border-radius: 0.128rem;
        color: white;
        padding: 0.55467rem 0.21333rem;
        vertical-align: middle;
        font-size: 0.59733rem;
    }
    #fetchcode{
        background:#898995;
        border-radius: 0.128rem;
        color: white;
        padding: 0.55467rem 0.21333rem;
        vertical-align: middle;
        font-size: 0.59733rem;
    }*/
</style>
<if condition="$step eq 1">
    <div class="loginsingup-input">
        <form method="post" >
            <div class="reset-pwd-title">短信修改支付密码</div>
            <div class="lsu">
                <span class="ico ico-telephone"></span>
                <input type="text" name="mobile" id="tel" value="{$user.mobile}" placeholder="请输入您的手机号" onBlur="checkMobilePhone(this.value);"/>
            </div>
            <div class="v-identity m-t-40">
                <span class="v-til">验证码 :</span>
                <div class="v-cont-wrap">
                    <input type="text" name="mobile_code" id="tpcode" value="" placeholder="请输入验证码">
                    <a  class="m_phone" href="javascript:void(0);" rel="mobile" id="fcode" onclick="sendcode(this)">获取验证码</a>
                </div>
            </div>
            <div class="lsu-submit">
                <input type="button" onclick="nextstep()" value="下一步" />
            </div>
        </form>
    </div>
</if>
<if condition="$step eq 2">
    <div class="loginsingup-input">
        <form  method="post"  id="payform">
            <if condition="!empty($user.paypwd)">
                <div class="reset-pwd-title">修改支付密码</div>
                <div class="lsu">
                    <span class="ico ico-old-password"></span>
                    <input type="password" name="old_password" id="old_password"  placeholder="请输入原密码"/>
                </div>
            </if>
            <div class="lsu">
                <span class="ico ico-password"></span>
                <input type="password" name="new_password" id="new_password"  placeholder="请输入新密码"/>
            </div>
            <div class="lsu">
                <span class="ico ico-password"></span>
                <input type="password" name="confirm_password" id="confirm_password"  placeholder="再次输入新密码" />
            </div>
            <div class="lsu-submit">
                <input type="button" onclick="submitverify()" value="确认修改" />
            </div>
        </form>
    </div>
</if>
<script>
    //手机验证
    function checkMobilePhone(mobile){
        if(mobile == ''){
            layer.open({content:'请输入您的手机号',time:2});
            return false;
        }else  if(!checkMobile(mobile)) {
            layer.open({content:'手机号码格式不正确！',time:2});
            return false;
        }
    }


    //发送短信验证码
    function sendcode(obj){
        var tel = $.trim($('#tel').val());
        var obj = $(obj);
        if(tel == ''){
            layer.open({content:'请输入您的号码！',time:2});
            return false;
        }
        var s = {$tpshop_config['sms_sms_time_out']|default=60};
        //改变按钮状态
        obj.unbind('click');
        //添加样式
        obj.attr('id','fetchcode');
        callback();
        //循环定时器
        var T = window.setInterval(callback,1000);
        function callback()
        {
            if(s <= 0){
                //移除定时器
                window.clearInterval(T);
                obj.bind('click',sendcode)
                obj.removeAttr('id','fetchcode');
                obj.text('获取验证码');
            }else{
                obj.text(--s + '秒后再获取');
            }
        }
        $.ajax({
            url : "/index.php?m=Home&c=Api&a=send_validate_code&scene=6&type=mobile&send="+tel,
            type:'post',
            dataType:'json',
            data:{type:obj.attr('rel'),send:tel},
            success:function(res){
                if(res.status==1){
                    //成功
                    layer.open({content:res.msg,time:2});
                }else{
                    //失败
                    layer.open({content:res.msg,time:2});
                    //移除定时器
                    window.clearInterval(T);
                    obj.removeAttr('id','fetchcode');
                    obj.text('获取验证码');
                }
            }
        })
    }

    //第一步验证
    function nextstep(){
        var tpcode = $('#tpcode').val();
        if(tpcode == ''){
            layer.open({content:'验证码不能为空',time:2});
            return false;
        }
        if(tpcode.length != 4){
            layer.open({content:'验证码错误',time:2});
            return false;
        }
        $.ajax({
            url:'/index.php?m=Home&c=Api&a=check_validate_code&t='+Math.random(),
            type:'post',
            dataType:'json',
            data:{type:$('#sender').val(),code:tpcode,send:$('#tel').val(),scene:6},
            success:function(data){
                if(data.status==1){
                    is_check = true;
                    window.location.href='/index.php?m=Mobile&c=User&a=paypwd&step=2&t='+Math.random();
                }else{
                    showErrorMsg(data.msg);
                    return false;
                }
            }
        })
    }

    //提交前验证表单
    function submitverify(){
        var new_password = $('#new_password').val();
        var confirm_password = $('#confirm_password').val();
        if(new_password == ''){
            layer.open({content:'新支付密码不能为空',time:2});
            return false;
        }
        if(new_password.length<6 || new_password.length>18){
            layer.open({content:'密码长度不符合规范',time:2});
            return false;
        }
        if(new_password != confirm_password){
            layer.open({content:'两次密码不一致',time:2});
            return false;
        }
        $.ajax({
            url:'/index.php?m=Mobile&c=User&a=paypwd&step=2&t='+Math.random(),
            type:'post',
            dataType:'json',
            data:$('#payform').serialize(),
            success:function(data){
                if(data.status==1){
                    layer.open({content: data.msg,time:2,end:function () {
                        window.location.href=data.url;
                    }});

                }else{
                    layer.open({content:data.msg,time:2});
                    return false;
                }
            }
        })
    }
</script>
</body>
</html>
